package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Cpxs;
import com.corpcredit.model.CwzbBgcy;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class CwzbBgcyForm extends BasePage implements Serializable {
    private GenericManager<CwzbBgcy, Long> cwzbBgcyManager;
    private CwzbBgcy cwzbBgcy = new CwzbBgcy();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setCwzbBgcyManager(GenericManager<CwzbBgcy, Long> cwzbBgcyManager) {
        this.cwzbBgcyManager = cwzbBgcyManager;
    }

    public CwzbBgcy getCwzbBgcy() {
        return cwzbBgcy;
    }

    public void setCwzbBgcy(CwzbBgcy cwzbBgcy) {
        this.cwzbBgcy = cwzbBgcy;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        cwzbBgcyManager.remove(cwzbBgcy.getId());
        addMessage("cwzbBgcy.deleted");

        return "list";
    }

    public String edit() {
    	 log.debug("enter edit:");
    	 if (id != null && id != 0) {
    		 cwzbBgcy = cwzbBgcyManager.get(id);
         } else {
        	 cwzbBgcy = new CwzbBgcy();
         }

         return "edit";
    }

    public String save() {
        boolean isNew = (cwzbBgcy.getId() == null || cwzbBgcy.getId() == 0);
        cwzbBgcy.setJbqk( jbqk );
        // set username who act this operation, get the user name from httprequest
        cwzbBgcy.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        cwzbBgcy.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        cwzbBgcy.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        cwzbBgcyManager.save(cwzbBgcy);

        String key = (isNew) ? "cwzbBgcy.added" : "cwzbBgcy.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            // set the id to the next page
            this.id = cwzbBgcy.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 